package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.ServletActionForward;

/**
 * Servlet implementation class Controller
 */
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public Controller() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("************************  Controller IN **************************************");
		
		IServletAction action = Factory.getAction(request.getRequestURI());
		
		ServletActionForward forward = action.execute(request, response);
		
		if(forward.getURL() != null) {
			if(forward.isValue())
				this.getServletContext().getRequestDispatcher(forward.getURL()).forward(request, response);
			else 
				response.sendRedirect(forward.getURL());
		}
			
	}

}
